package com.ruoyi.shop.popularize.batch.controller;

import java.util.*;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.mall.common.domain.DtsSeckillBatch;
import com.ruoyi.shop.popularize.batch.service.IDtsSeckillBatchService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 秒杀商品列表Controller
 * 
 * @author ruoyi
 * @date 2025-03-14
 */
@RestController
@RequestMapping("/popularize/batch")
public class DtsSeckillBatchController extends BaseController
{
    @Autowired
    private IDtsSeckillBatchService dtsSeckillBatchService;

    /**
     * 查询秒杀商品列表列表
     */
    @PreAuthorize("@ss.hasPermi('popularize:batch:list')")
    @GetMapping("/list")
    public TableDataInfo list(DtsSeckillBatch dtsSeckillBatch)
    {
        startPage();
        List<DtsSeckillBatch> list = dtsSeckillBatchService.selectDtsSeckillBatchList(dtsSeckillBatch);
        return getDataTable(list);
    }

    /**
     * 导出秒杀商品列表列表
     */
    @PreAuthorize("@ss.hasPermi('popularize:batch:export')")
    @Log(title = "秒杀商品列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DtsSeckillBatch dtsSeckillBatch)
    {
        List<DtsSeckillBatch> list = dtsSeckillBatchService.selectDtsSeckillBatchList(dtsSeckillBatch);
        ExcelUtil<DtsSeckillBatch> util = new ExcelUtil<DtsSeckillBatch>(DtsSeckillBatch.class);
        util.exportExcel(response, list, "秒杀商品列表数据");
    }

    /**
     * 获取秒杀商品列表详细信息
     */
    @PreAuthorize("@ss.hasPermi('popularize:batch:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(dtsSeckillBatchService.selectDtsSeckillBatchById(id));
    }

    /**
     * 根据商品规格id查询秒杀价格
     */
    @PreAuthorize("@ss.hasPermi('popularize:batch:productId')")
    @PostMapping( "/productId")
    public AjaxResult productId(@RequestBody Long[] productIds)
    {
        String productId = "";
        for (int i = 0; i < productIds.length; i++) {
            // 循环进行拼接
            if (i == productIds.length - 1) {
                productId += productIds[i];
            } else {
                productId += productIds[i] + ",";
            }
        }
        return success(dtsSeckillBatchService.selectDtsSeckillBatchBySpecId(productId));
    }

    /**
     * 新增或修改秒杀商品列表
     */
    @PreAuthorize("@ss.hasPermi('popularize:batch:add')")
    @Log(title = "秒杀商品列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult addOrUpdate(@RequestBody Map<String, Object> data) {
        return dtsSeckillBatchService.addOrUpdate(data);
    }

    /**
     * 修改秒杀商品列表
     */
    @PreAuthorize("@ss.hasPermi('popularize:batch:edit')")
    @Log(title = "秒杀商品列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DtsSeckillBatch dtsSeckillBatch)
    {
        return toAjax(dtsSeckillBatchService.updateDtsSeckillBatch(dtsSeckillBatch));
    }

    /**
     * 修改秒杀商品的结束状态为结束
     */
    @PreAuthorize("@ss.hasPermi('popularize:batch:conclude')")
    @Log(title = "秒杀商品列表", businessType = BusinessType.UPDATE)
    @PutMapping("/id")
    public AjaxResult conclude(Long id)
    {
        return toAjax(dtsSeckillBatchService.updateConclude(id));
    }

    /**
     * 删除秒杀商品列表
     */
    @PreAuthorize("@ss.hasPermi('popularize:batch:remove')")
    @Log(title = "秒杀商品列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(dtsSeckillBatchService.deleteDtsSeckillBatchByIds(ids));
    }

    /**
     * 检查商品规格是否存在于秒杀表中
     */
    @PostMapping("/checkProduct")
    public AjaxResult checkProduct(@RequestBody List<Integer> productIds) {
        Map<Integer, Boolean> existMap = new HashMap<>();
        for (Integer productId : productIds) {
            // 根据product_id查询秒杀表中是否存在记录
            DtsSeckillBatch seckillBatch = dtsSeckillBatchService.selectDtsSeckillBatchByProductId(productId);
            existMap.put(productId, seckillBatch != null);
        }
        return AjaxResult.success(existMap);
    }
}
